package com.twu.biblioteca.service;

import com.twu.biblioteca.IMovieDao;
import com.twu.biblioteca.modal.Movie;

public class MovieService {
    private static final String format = "| %-6d | %-20s | %-4d | %-17s | %-4s |%n";
    private final String separateLine = String.format("+--------+----------------------+------+-------------------+------+%n");
    private IMovieDao movieDao;

    public void setMovieDao(IMovieDao movieDao) {
        this.movieDao = movieDao;
    }

    public String listMovies() {
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(separateLine).append(String.format("|Movie ID|      Moive Name      | Year |       Director    | Rate |%n")).append(separateLine);

        for (Movie movie : movieDao.getMovies()) {
            stringBuilder.append(String.format(format, movie.getId(), movie.getName(), movie.getYear(), movie.getDirector(), movie.getRate()));
        }

        stringBuilder.append(separateLine);
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }

    public boolean checkout(int movieID) {
        if (movieID <= movieDao.getMovies().size()) {
            return true;
        }
        return false;
    }
}
